import videojs from "video.js";
import 'video.js/dist/video-js.css'

class Video {
    constructor() {
        
    }
    /**
     * 视频解析为source格式
     * @param {播放地址} url
     */
    videoUrlToSource(url) {
        let type = ''
        const lastKey = url.split('.').pop()
        switch (lastKey) {
          case 'm3u8': {
            type = 'application/x-mpegURL'
            break
          }
          case 'mp4': {
            type = 'video/mp4'
            break
          }
          default: {
            if (lastKey.length > 4) {
              type = 'video/mp4'
              break
            } else {
              type = `video/${lastKey}`
              break
            }
          }
        }
        return {
          src: url,
          type
        }
    }
    /**
     *初始化video
     * @param {*视频盒子节点} el
     * @param {*视频地址} url
     */
     videoInit(el, url) {
        if (!el || !url) return
        el.innerHTML = ''
        el.innerHTML = `<video id="my-video" class="video-js"></video>`
        emit('initStart')
        try {
          videoInstance.value = markRaw(
            videojs('my-video', {
              controls: true, // 是否显示控制条
              preload: 'auto',
              autoplay: false,
              muted: false, // 是否静音
              fluid: true,
              sources: [videoUrlToSource(url)]
            })
          )
          emit('initSuccessed')
          return videoInstance.value
        } catch (err) {
          emit('initFailed')
          console.log(err, 'init')
          return null
        }
      }
}